package com.hui.server.controller;


import com.hui.server.entity.Salary;
import com.hui.server.service.SalaryService;
import com.hui.server.util.Result;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.List;

/**
 * <p>
 * 前端控制器
 * </p>
 * 工资账套，工资模板，每个员工的工资模板都是不一样的
 *
 * @author hui
 * @since 2022-03-31
 */
@RestController
@RequestMapping("/salary/sob")
public class SalaryController {

    private final SalaryService salaryService;


    @Autowired
    public SalaryController(SalaryService salaryService) {
        this.salaryService = salaryService;
    }


    @ApiOperation(value = "获取所有工资套账")
    @GetMapping("/")
    public Result<?> getAllSalary() {
        List<Salary> salaryList = salaryService.list();
        return Result.success(salaryList);
    }

    @ApiOperation(value = "添加工资套账")
    @PostMapping("/")
    public Result<?> addSalary(@RequestBody Salary salary) {
        boolean save = salaryService.save(salary);
        return save ?
                Result.success() :
                Result.error(String.valueOf(HttpStatus.INTERNAL_SERVER_ERROR.value()), "添加失败！");
    }

    @ApiOperation(value = "删除工资套账")
    @DeleteMapping("/{id}")
    public Result<?> deleteSalary(@PathVariable Integer id) {
        boolean remove = salaryService.removeById(id);
        return remove ?
                Result.success() :
                Result.error(String.valueOf(HttpStatus.INTERNAL_SERVER_ERROR.value()), "删除失败！");
    }

    @ApiOperation(value = "更新工资套账")
    @PutMapping("/")
    public Result<?> updateSalary(@RequestBody Salary salary) {
        boolean update = salaryService.updateById(salary);
        return update ?
                Result.success() :
                Result.error(String.valueOf(HttpStatus.INTERNAL_SERVER_ERROR.value()), "删除失败！");
    }


}
